﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.Data;

public partial class Admin_ucControl_ucTinTucSuKien : System.Web.UI.UserControl
{
    TblTinTucSuKienDAO tintucSKDAO = new TblTinTucSuKienDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            if (id != null || id != "")
            {
                loadDetail();
            }
        }
    }

    private void loadDetail()
    {
        string id = Request.QueryString["id"];
        DataTable dt = tintucSKDAO.GetTinTucSK_ByID(Global.ConvertToInt(id));
        foreach (DataRow dr in dt.Rows)
        {
            txtTieuDe.Text = dr["TieuDe"].ToString();
            txtDateTime.Text = dr["Datetime"].ToString();
            FCKNoiDung.Value = dr["NoiDung"].ToString();
            noidungtomtat.Content = dr["NoiDungTomTat"].ToString();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblTinTucSuKien tintucSK = new TblTinTucSuKien();
            tintucSK.Datetime = Convert.ToDateTime(txtDateTime.Text.ToString());
            tintucSK.TieuDe = txtTieuDe.Text;
            tintucSK.NoiDung = FCKNoiDung.Value;

            tintucSK.NoiDungTomTat = Server.HtmlDecode(noidungtomtat.Content);

            string path = Server.MapPath("~\\Admin\\img\\Items\\");
            string filename = img.FileName;
            if (System.IO.File.Exists(path + filename))
            {

                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            else
            {
                filename = filename.ToString();
            }
            img.SaveAs(path + filename);

            tintucSK.Image = filename;

            string id = Request.QueryString["id"];
            if (id == null || id == "")
            {
                tintucSKDAO.InsUpTinTucSuKien(tintucSK, 1);
            }
            else
            {
                tintucSK.ID = Global.ConvertToInt(id);
                tintucSKDAO.InsUpTinTucSuKien(tintucSK, 2);
            }
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/DSTinTucSuKien.aspx");
        }
    }
  
}